$(function(){
    layui.config({
        base: Think.__JS__ + '/layui/',
        version: '101100'
    }).extend({
        'dltable': 'layui_ext/treeGrid/dltable',
        'common': '../common',
        'setter': '../setter'
    }).use(['dltable', 'common'], function () {
        var $ = layui.jquery
            ,dltable = null
            ,tableId = 'treeTable'
            ,defaultFunc = function () {} // 默认函数
        dltable = layui.dltable;//很重要
        var onToolbar = $('script[role="onToolbar"]');
        var onCheckbox = $('script[role="onCheckbox"]');
        var onEdit = $('script[role="onEdit"]');
        var onRow = $('script[role="onRow"]');
        var onTool = $('script[role="onTool"]');
        var onSort = $('script[role="onSort"]');

        $('script[role="treeGrid"]').each(function (index, elem) {
            var str = $(this).html(), opt = {};
            if (str.length > 0)
                opt = window.ICY8.str2json(str);
            // 默认配置
            var option = {
                id: tableId
                , elem: '#' + tableId
                , cellMinWidth: 100
                , idField: 'id'//必須字段
                , isTree: true // 是否开启树形表格
                , treeId: 'id'//树形id字段名称
                , treeUpId: 'pid'//树形父id字段名称
                , treeShowName: 'name'//以树形式显示的字段
                , heightRemove: [".dHead", 10]//不计算的高度,表格设定的是固定高度，此项不生效
                , height: '100%'
                , isFilter: false
                , iconOpen: true//是否显示图标【默认显示】
                , isOpenDefault: true//节点默认是展开还是折叠【默认展开】
                , loading: true
                , method: 'post'
                , isPage: false
                , parseData: function (res) {//数据加载后回调
                    return res;
                }
                , onClickRow: function (index, o) {
                }
                , onDblClickRow: function (index, o) {
                },
                branch:['&nbsp;','&nbsp;'],
                leaf:'&nbsp;',
            };
            option.limit = 10;
            option.limits = [];
            var i = 0;
            $.extend(true, option, opt);
            // 自动计算分页
            if (option.limits.length>0) {
                while (i < option.limits.length) {
                    option.limits[i] = (i + 1) * option.limit;
                    i++;
                }
            } else {
                while (i < 10) {
                    option.limits[i] = (i + 1) * option.limit;
                    i++;
                }
            }
            if (opt.id && !opt.elem) {
                option.elem = '#' + option.id;
            }
            var ptable = dltable.render(option);
            // 各事件
            option.onToolbar = window.ICY8.str2json($(onToolbar[index]).html());
            option.onCheckbox = window.ICY8.str2json($(onCheckbox[index]).html());
            option.onEdit = window.ICY8.str2json($(onEdit[index]).html());
            option.onRow = window.ICY8.str2json($(onRow[index]).html());
            option.onTool = window.ICY8.str2json($(onTool[index]).html());
            option.onSort = window.ICY8.str2json($(onSort[index]).html());

            dltable.on('toolbar(' + option.id + ')', (typeof option.onToolbar == 'function') ? option.onToolbar : defaultFunc);
            dltable.on('checkbox(' + option.id + ')', (typeof option.onCheckbox == 'function') ? option.onCheckbox : defaultFunc);
            dltable.on('edit(' + option.id + ')', (typeof option.onEdit == 'function') ? option.onEdit : defaultFunc);
            dltable.on('row(' + option.id + ')', (typeof option.onRow == 'function') ? option.onRow : defaultFunc);
            dltable.on('tool(' + option.id + ')', (typeof option.onTool == 'function') ? option.onTool : defaultFunc);
            dltable.on('sort(' + option.id + ')', (typeof option.onSort == 'function') ? option.onSort : defaultFunc);
        });
    });
});
